package Action;

import Entities.DoeBookEntity;
import Entities.SimpleBookEntity;
import Service.RecommendService;
import Service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.util.List;
import java.util.Map;

//控制登录的Action
public class LoginAction extends ActionSupport
{
    private String user_name;
    private String user_password;
    private UserService userService;

    //原RecommendAction里的属性
    private RecommendService recommendService;
    private List<DoeBookEntity> recommendList;
    private int initLimit = 14;
    private String after_id = "0";
    private String limit=String.valueOf(initLimit);
    private List<DoeBookEntity> feedList;

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getUser_password() {
        return user_password;
    }

    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public RecommendService getRecommendService() {
        return recommendService;
    }

    public void setRecommendService(RecommendService recommendService) {
        this.recommendService = recommendService;
    }

    public List<DoeBookEntity> getRecommendList() {
        return recommendList;
    }

    public void setRecommendList(List<DoeBookEntity> recommendList) {
        this.recommendList = recommendList;
    }

    public int getInitLimit() {
        return initLimit;
    }

    public void setInitLimit(int initLimit) {
        this.initLimit = initLimit;
    }

    public String getAfter_id() {
        return after_id;
    }

    public void setAfter_id(String after_id) {
        this.after_id = after_id;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

    public List<DoeBookEntity> getFeedList() {
        return feedList;
    }

    public void setFeedList(List<DoeBookEntity> feedList) {
        this.feedList = feedList;
    }

    //判断登录是否成功
    public String execute()
    {
        Map<String,Object> session =  ActionContext.getContext().getSession();
        session.remove("userNameOrPassword");
        boolean flag = false;
        //这个if和else-if中的内容可以在客户端判断
        if(user_name == null || user_name.trim().equals(""))
        {
            this.addFieldError("user_name","用户名不能为空");
            return "fail";
        }
        else if(user_password == null || user_password.trim().equals(""))
        {
            this.addFieldError("user_password","密码不能为空");
            return "fail";
        }
        else {
//            ApplicationContext ctx = new ClassPathXmlApplicationContext("WEB-INF/applicationContext.xml");
//            userService = (UserService) ctx.getBean("userService");
            //上面两行代码是没有必要的，因为那样的话重新通过配置文件new了一个ApplicationContext对象，这样是非常耗时间耗空间的，
            //把spring和struts2整合即可不必new这个对象。只需要先在applicationContext.xml中配置Spring监听器，
            // 再在web.xml中配置context-param参数（使spring通过匹配给定模式文件来初始化对象），
            //最后再再struts.xml中把struts2交给spring托管就行了。

            flag = userService.checkUser(user_name, user_password, 1);  //检查用户名密码是否正确
            if (flag == false)
            {
//                this.addFieldError("user_nameOrPassword", "用户名或密码错误");
                session.put("userNameOrPassword","用户名或密码错误");
                return "fail";
            }
            else
            {
                //用户名密码正确，成功

                //把用户名存入session
                session =  ActionContext.getContext().getSession();
                session.put("user_name", user_name);
                session.put("isLogin",true);
                session.put("sessionid",ServletActionContext.getRequest().getSession().getId());

                return "success";
            }
        }
    }




}
